package com.cpi.cornpp.recurrentcharges.service;

import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.recurrentcharges.converter.RecurrentChargesConverter;
import com.cpi.cornpp.recurrentcharges.dao.RecurrentChargesDAO;
import com.cpi.cornpp.recurrentcharges.dto.RecurrentChargesAddDto;
import com.cpi.cornpp.recurrentcharges.dto.RecurrentChargesDTO;
import com.cpi.cornpp.recurrentcharges.exception.RecurrentChargesException;
import com.cpi.cornpp.recurrentcharges.view.RecurrentChargesAddView;
import com.cpi.cornpp.recurrentcharges.view.RecurrentChargesAmountView;

public class RecurrentChargesService {
	
	public List getRecurrentCharges(int versionId)throws RecurrentChargesException{
		List recurrentChargesGroupViews = null;
		List recurrentChargesDTOs = null;
		RecurrentChargesDAO recurrentChargesDAO = null;
		RecurrentChargesConverter recurrentChargesConverter = null;
		try{
			recurrentChargesDAO = new RecurrentChargesDAO();
			recurrentChargesConverter = new RecurrentChargesConverter();
			recurrentChargesDTOs = recurrentChargesDAO.getRecurrenceCharges(versionId);
			recurrentChargesGroupViews = recurrentChargesConverter.convertDTOsTOViews(recurrentChargesDTOs);
		}catch(RecurrentChargesException recurrentChargesException){
			throw recurrentChargesException;
		}catch(Exception exception){
			RecurrentChargesException recurrentChargesException = null;
			exception.printStackTrace(System.out);
			recurrentChargesException = new RecurrentChargesException(exception, CornPPException.LAYER_SERVICE, CornPPException.ACTION_LISTS);
			throw recurrentChargesException;
		}
		return recurrentChargesGroupViews;
	}
	
	public void updateRecurrentCharges(List recurrentChargesAmountViews)throws RecurrentChargesException {
		RecurrentChargesDAO recurrentChargesDAO = null;
		RecurrentChargesConverter recurrentChargesConverter = null;
		Iterator iterator = null;
		RecurrentChargesAmountView recurrentChargesAmountView = null;
		RecurrentChargesDTO recurrentChargesDTO = null; 
		try{
			recurrentChargesDAO = new RecurrentChargesDAO();
			recurrentChargesConverter = new RecurrentChargesConverter();
			iterator = recurrentChargesAmountViews.iterator();
			while( iterator.hasNext() ){
				recurrentChargesAmountView = (RecurrentChargesAmountView)iterator.next();
				recurrentChargesDTO = recurrentChargesConverter.convertViewToDTO(recurrentChargesAmountView);
				recurrentChargesDAO.update(recurrentChargesDTO);
			}
		}catch(RecurrentChargesException recurrentChargesException){
			throw recurrentChargesException;
		}catch(Exception exception){
			RecurrentChargesException recurrentChargesException = null;
			exception.printStackTrace(System.out);
			recurrentChargesException = new RecurrentChargesException(exception, CornPPException.LAYER_SERVICE, CornPPException.ACTION_UPDATE);
			throw recurrentChargesException;
		}
	}
	
	public void deleteRecurrentCharges(RecurrentChargesAddView recurrentChargesAddView)throws RecurrentChargesException{
		RecurrentChargesDAO recurrentChargesDAO = null;
		RecurrentChargesConverter recurrentChargesConverter = null;
		RecurrentChargesAddDto recurrentChargesAddDto = null;
		try{
			recurrentChargesDAO = new RecurrentChargesDAO();
			recurrentChargesConverter = new RecurrentChargesConverter();
			recurrentChargesAddDto = recurrentChargesConverter.convertViewToDTO(recurrentChargesAddView);
			recurrentChargesDAO.delete(recurrentChargesAddDto);
		}catch(RecurrentChargesException recurrentChargesException){
			throw recurrentChargesException;
		}catch(Exception exception){
			RecurrentChargesException recurrentChargesException = null;
			exception.printStackTrace(System.out);
			recurrentChargesException = new RecurrentChargesException(exception, CornPPException.LAYER_SERVICE, CornPPException.ACTION_DELETE);
			throw recurrentChargesException;
		}
	}
	/**
	 * 
	 * @param view
	 * @return
	 * @throws RecurrentChargesException
	 */
	public boolean existRecurrentCharges(RecurrentChargesAddView view)throws RecurrentChargesException {
		boolean exists = false;
		RecurrentChargesConverter converter = null;
		RecurrentChargesDAO dao = null;
		RecurrentChargesAddDto dto = null;

		try {
			converter = new RecurrentChargesConverter();
			dto = converter.convert(view);
			dao = new RecurrentChargesDAO();
			exists = dao.getRecurrentChargePlantTransportCost(dto);
		} catch (RecurrentChargesException rcException) {
			throw rcException;
		} catch (Exception e) {
			RecurrentChargesException rcException = null;
			e.printStackTrace(System.out);
			rcException = new RecurrentChargesException(e, CornPPException.LAYER_SERVICE, CornPPException.ACTION_SELECT);
			throw rcException;
		}
		return exists;
	}
	
	/**
	 * 
	 * @param view
	 * @throws RecurrentChargesException
	 */
	public void insertRecurrentCharges(RecurrentChargesAddView view)throws RecurrentChargesException {
		RecurrentChargesConverter converter = null;
		RecurrentChargesDAO dao = null;
		RecurrentChargesAddDto dto = null;
		try {
			converter = new RecurrentChargesConverter();
			dao = new RecurrentChargesDAO();
			dto = converter.convert(view);
			dao.insert(dto);
		} catch (RecurrentChargesException rcException) {
			throw rcException;
		}catch (Exception e) {
			RecurrentChargesException rcException = null;
			e.printStackTrace(System.out);
			rcException = new RecurrentChargesException(e, CornPPException.LAYER_SERVICE, CornPPException.ACTION_INSERT);
			throw rcException;
		}
	}
}
